home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / share / audacity / plug-ins / SoundFinder.ny < prev    next >
Encoding:
Audacity Nyquits plug-in  |  2010-09-21  |  6.2 KB  |  147 lines

  1. ;nyquist plug-in
  2. ;version 1
  3. ;type analyze
  4. ;categories "http://lv2plug.in/ns/lv2core#AnalyserPlugin"
  5. ;name "Sound Finder..."
  6. ;action "Finding sound..."
  7. ;info "Written by Jeremy R. Brown (http://www.jeremy-brown.com/) \nbased on the Silence Finder script by\nAlex S. Brown, PMP (http://www.alexsbrown.com) \n\Released under terms of the GNU General Public License version 2\nAdds region labels for areas of sound according to the specified level\nand duration of surrounding silence. If too many labels are produced,\nincrease the silence level and duration; if too few are produced,\nreduce the level and duration."
  8. ;control sil-lev "Treat audio below this level as silence [ -dB]" real "" 26 0 100
  9. ;control sil-dur "Minimum duration of silence between sounds [seconds]" real "" 1.0 0.1 5.0
  10. ;control labelbeforedur "Label starting point [seconds before sound starts]" real "" 0.1 0.0 1.0
  11. ;control labelafterdur "Label ending point [seconds after sound ends]" real "" 0.1 0.0 1.0
  12. ;control finallabel "Add a label at the end of the track? [No=0, Yes=1]" int "" 0 0 1
  13.  
  14. ;30Dec09: couple of changes made to default control values by Gale Andrews 
  15.  
  16. ;Create a function to make the sum the two channels if they are stereo
  17. (defun mono-s (s-in) (if (arrayp s-in) (snd-add (aref s-in 0) (aref s-in 1))
  18. s-in))
  19.  
  20. ;Create a function to reduce the sample rate and prepare the signal for
  21. ;analysis. RMS is good to monitor volume the way humans hear it, but is not
  22. ;available in Audacity. Used a peak-calculating function instead.
  23. ;NOTE: this is the place to add any processing to improve the quality of the
  24. ;signal. Noise filters could improve the quality of matches for noisy signals.
  25. ;PERFORMANCE vs. ACCURACY
  26. ;Reducing the samples per second should improve the performance and decrease
  27. ;the accuracy of the labels. Increasing the samples per second will do the
  28. ;opposite. The more samples checked, the longer it takes. The more samples
  29. ;checked, the more precisely the program can place the silence labels.
  30. ;my-srate-ratio determines the number of samples in my-s. Set the number after (snd-srate s)
  31. ;higher to increase the number of samples.
  32.  
  33. (defun my-s (s-in)
  34.  (setq my-srate-ratio (truncate (/ (snd-srate (mono-s s-in)) 100)))
  35.  (snd-avg (mono-s s-in) my-srate-ratio my-srate-ratio OP-PEAK)
  36. )
  37.  
  38. ;Set the silence threshold level (convert it to a linear form)
  39. (setq thres (db-to-linear (* -1 sil-lev)))
  40. ;Store the sample rate of the sound
  41. (setq s1-srate (snd-srate (my-s s)))
  42. ;Initialize the variable that will hold the length of the sound.
  43. ;Do not calculate it now with snd-length, because it would waste memory.
  44. ;We will calculate it later.
  45. (setq s1-length 0)
  46. ;Initialize the silence counter
  47. (setq sil-c 0)
  48. ;Initialize the labels variable
  49. (setq l NIL)
  50. ;Convert the silence duration in seconds to a length in samples
  51. (setq sil-length (* sil-dur s1-srate))
  52.  
  53. ;Set the sound-start marker to -1, indicating no sound has been found yet
  54. (setq sound-start -1)
  55. (setq silence-start -1)
  56. ;Set the flag that says we are looking for the start of a sound (as opposed to the start of a silence)
  57. (setq sound-search 1)
  58. ;Set the counter that counts sounds
  59. (setq sound-count 0)
  60.  
  61. (setq label-string "")
  62.  
  63. ;Define a function to add new items to the list of labels
  64. (defun add-label (l-starttime l-endtime l-text)
  65.  (setq l (cons (list l-starttime l-endtime l-text) l))
  66. )
  67.  
  68. ;The main working part of the program, it counts
  69. ;the number of sequential samples with volume under
  70. ;the threshold. It adds to a list of markers every time
  71. ;there is a longer period of silence than the silence
  72. ;duration amount.
  73.  
  74. ;It runs through a loop, adding to the list of markers (l)
  75. ;each time it finds silence.
  76. (let (s1) ;Define s1 as a local variable to allow efficient memory use
  77.  ; Get the sample into s1, then free s to save memory
  78.  (setq s1 (my-s s))
  79.  (setq s nil)
  80.  ;Capture the result of this "do" loop, because we need the sound's length
  81.  ;in samples.
  82.  (setq s1-length
  83.   ;Keep repeating, incrementing the counter and getting another sample
  84.   ;each time through the loop.
  85.   (do ((n 1 (+ n 1)) (v (snd-fetch s1) (setq v (snd-fetch s1))))
  86.    ;Exit when we run out of samples (v is nil) and return the number of
  87.    ;samples processed (n)
  88.    ((not v) n)
  89.    ;Start the execution part of the do loop
  90.    
  91.    ;if found silence, increment the silence counter; if silence-start is not already > -1, set the start of silence to the current sample number (n)
  92.    (if (< v thres) 
  93.       (progn
  94.          (setq sil-c (+ sil-c 1))
  95.          (if (= silence-start -1) (setq silence-start n))
  96.       )
  97.    )
  98.    
  99.    ;if found sound, and sound-search is 1, mark the start of the sound and change sound-search to 0 (look for silence next)
  100.    (if (and (>= v thres) (= sound-search 1))
  101.       (progn
  102.          (setq sound-search 0)
  103.          (setq sound-start n)
  104.          (setq sound-count (1+ sound-count))
  105.       )
  106.    )
  107.  
  108.    ;if found silence, and silence-counter is long enough, and sound-search is 0, and sound-start is not -1, and silence-start is not -1, that indicates the end of a sound (for which we have already found the beginning), which we should now label
  109.    (if (and (< v thres) (= sound-search 0) (/= sound-start -1) (> sil-c sil-length) (/= silence-start -1))
  110.       (progn
  111.          (setq sound-search 1)
  112.          (setq sil-c 0)
  113.          ;Create the label text
  114.          (setq label-string (strcat label-string (format nil "~A" sound-count)))
  115.          (add-label (- (/ sound-start s1-srate) labelbeforedur) (+ (/ silence-start s1-srate) labelafterdur) label-string)
  116.          (setq label-string "")
  117.          (setq silence-start -1)
  118.       )
  119.    )
  120.    
  121.    ;if found sound, reset the silence-counter and silence-start
  122.    (if (>= v thres) 
  123.       (progn
  124.          (setq sil-c 0)
  125.          (setq silence-start -1)
  126.       )
  127.    )
  128.   )
  129.  )
  130. )
  131.  
  132. ;if we're still looking for the end of a sound at the end of the file, end the sound at the end of the file
  133. (if (= sound-search 0)
  134.    (progn
  135.       (setq label-string (strcat label-string (format nil "~A" sound-count)))
  136.       (add-label (- (/ sound-start s1-srate) labelbeforedur) (/ s1-length s1-srate) label-string)
  137.    )
  138. )
  139.  
  140. ;If no sound markers were found, return a message
  141. ;Otherwise, if some sounds were found, also optionally place a label at the end of the file.
  142. (if (null l)
  143.  (setq l "No sounds found. Try reducing the silence\nlevel and minimum silence duration.")
  144.  (if (= finallabel 1) (add-label (/ s1-length s1-srate) (/ s1-length s1-srate) "[End]"))
  145. )
  146. l
  147.